package com.lohika.util
{
	import com.lohika.Constant;
	
	import events.GameEvent;
	
	import fl.motion.easing.Bounce;
	import fl.transitions.Tween;
	
	import flash.display.*;
	import flash.events.MouseEvent;
	import flash.filters.DropShadowFilter;
	import flash.net.SharedObject;
	import flash.text.*;
	
	public class Window extends Sprite
	{		
		private static var instance:Window;
		private static var storage:SharedObject = SharedObject.getLocal("bestGameResult");
		
		private var label:TextField; 
		private var buttonBar:Sprite;
		private static var moveTween:Tween;
		
		public static function show(parent:Sprite, modal:Boolean = false):void
		{
			if ( !instance )
			{
				instance = new Window();
				parent.addChild(instance)
			}
				
			if (modal)
			{
				const g:Graphics = instance.graphics;
				g.clear();
				g.beginFill(0x0, .5); 
				g.drawRect(0, 0, Main.stageWidth, Main.stageHeight); //screen
				g.endFill();
			}
		}
		
		public static function remove():void
		{
			
		}
		
		public function Window()
		{
			super();
			visible = false;
			
			drawBackground();
			
			this.y = (Main.stageHeight - this.height) / 2;
			
			
			label = new TextField();
			label.width = this.width;
			label.height = 250;
			label.defaultTextFormat = new TextFormat("Tahoma", 22, 0xFFFFFF, true, null, null, null, null, TextFormatAlign.CENTER, 15, 15 );
			addChild(label);

			buttonBar = new Sprite();
			buttonBar.y = 150;
			buttonBar.addChild( new ButtonBack() )
			buttonBar.addChild( new ButtonRestart() );
			for (var i:int = 0; i < buttonBar.numChildren; i++) 
			{
				var btn:SimpleButton = buttonBar.getChildAt(i) as SimpleButton;
				btn.x = (btn.width + 50) *i + 95;
			}			
			buttonBar.addEventListener(MouseEvent.CLICK, onRestartGame);
			addChild(buttonBar);
		}
		
		/**
		 * Save game result to Coockie 
		*/
		private function save(score:uint):void
		{
			var last:uint = ( storage.data.score ) ?  storage.data.score : 0;
			storage.data.score = Math.max(last, score);
			storage.flush();
		}

		
		public function appear(score:uint, mode:uint):void
		{
			buttonBar.getChildAt(0).visible = (mode == Constant.PAUSE); //"Back" button
			
			if (mode == Constant.GAME_OVER)
			{
				save(score);
			}
			
			label.text =  getText(score, mode);
			var endX:Number = (Main.stageWidth - this.width) / 2;;
			moveTween = new Tween(this, "x" , Bounce.easeOut, - 200, endX, .5, true);
			
			visible = true;
		}
		
		public function disappear():void
		{
			visible = false;
		}
		
		protected function onRestartGame(event:MouseEvent):void
		{
			event.stopPropagation();
			if (event.target is ButtonBack)
			{
				dispatchEvent(new GameEvent(GameEvent.CONTINUED, 0, true));
			}
			else if (event.target is ButtonRestart)
			{
				disappear();
				dispatchEvent(new GameEvent(GameEvent.RESTART, 0, true));
			}
		}
		
		
		private function getText(score:uint, mode:uint):String
		{
			var result:String = (mode == Constant.GAME_OVER) ? "::GAME OVER::" : "::GAME PAUSED::";
			var last:uint = ( storage.data.score ) ?  storage.data.score : 0;
			result += "\n\nBest score: " +  last;
			if (mode == Constant.GAME_OVER)
				result += "\nLast score: " + score;
			
			return result;
		}
		
		private function drawBackground():void
		{
			//draw border
			with (graphics)
			{
				clear()
				lineStyle(5,0xC0C0C0)
				beginFill(0x0080FF , 0.25);
				drawRoundRect(0, 0, 300, 175, 15);
				endFill();
			}
			
			filters = [ new DropShadowFilter(8) ];
		}
		

	}
}